package com.sktlab.bizconfmobile.activity;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.ExecutorService;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.sktlab.bizconfmobile.R;
import com.sktlab.bizconfmobile.model.AccessNumber;
import com.sktlab.bizconfmobile.model.ConferenceAccount;
import com.sktlab.bizconfmobile.model.Constant;
import com.sktlab.bizconfmobile.model.manager.AccountsManager;
import com.sktlab.bizconfmobile.model.manager.ConfHistoryManager;
import com.sktlab.bizconfmobile.model.manager.MeetingManager;
import com.sktlab.bizconfmobile.model.manager.ContactManager;
import com.sktlab.bizconfmobile.plistparse.SAXParse;
import com.sktlab.bizconfmobile.util.Util;

/**
 * @Description: welcom screen
 * 
 * @Author wenjuan.li
 * 
 * @date 2013-07-23
 * 
 * @version V1.0
 */
public class WelcomeActivity extends Activity {
	
	public static final String TAG = "WelcomeActivity"; 
	private final int SHOW_MAIN_ACTIVITY = 1;
	private long beginTime = 0;
	private Context mCtx;
	private AssetManager am;
	
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {

			switch (msg.what) {

			case SHOW_MAIN_ACTIVITY:

				startMainActivity();
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		
		//Util.BIZ_CONF_DEBUG(TAG, "onCreate called~");
		setFullScreen();
		setContentView(R.layout.welcomelogo);
		
		init();
	}
	
	private void init(){
		
		//add some object to this array list, when user clear the phone's memory, the application's memory was cleared
		//too, then we should restart the application to malloc memory.
		//this list is a signal, when application start, add some object to it,if we find its size to 0,we know,our app's
		//memory had been cleared, we should restart our app
		if(AppClass.signal.size() <= 0){
			
			AppClass.signal.add(new ConferenceAccount());
			AppClass.signal.add(new ConferenceAccount());
			AppClass.signal.add(new ConferenceAccount());
		}
		
		mCtx = this;		
		am = getAssets();
		
		beginTime = System.currentTimeMillis();
		
		loadData();	
	}

	public void loadData() {

		ExecutorService service = AppClass.getInstance().getService();

		service.submit(new Runnable() {

			@Override
			public void run() {
				
				//This can only be called after ModelManager.initInstance()	
				AccountsManager.getInstance().loadAccountFromDb();
				MeetingManager.getInstance().loadMeetingFromDb();	
				ConfHistoryManager.getInstance().loadHistoryFromDb();
				ContactManager.getInstance().Load();
				
				loadAccessNumber();
				
				Long usedTime = System.currentTimeMillis() - beginTime;

				Long sleepTime = Constant.WELCOME_SCREEN_SHOW_TIME - usedTime;

				if (sleepTime <= 0) {

					mHandler.sendEmptyMessage(SHOW_MAIN_ACTIVITY);
				} else {

					mHandler.sendEmptyMessageDelayed(SHOW_MAIN_ACTIVITY, sleepTime);
				}
			}
		});
	}
	
	public void loadAccessNumber() {
		
		if (!Util.isEmpty(am)) {
			
			try {
				Locale locale = Locale.getDefault();
				String language = String.format("%s_%s", 
										locale.getLanguage().toLowerCase(), 
										locale.getCountry().toLowerCase());
				
				//Util.BIZ_CONF_DEBUG(TAG, "language: " + language);
				
				InputStream in = null;
				
				if (language.equalsIgnoreCase(Constant.LOCAL_ZH)) {

					in = am.open(Constant.ACCESS_NUMBER_LIST_CH);
				} else {

					in = am.open(Constant.ACCESS_NUMBER_LIST_EN);
				}
				
				SAXParse parser =  new SAXParse();
				List<AccessNumber> list = parser.getAccessNumberList(in);
				
				AppClass.getInstance().setAccessNumberList(list);
			} catch (IOException e) {
				
				e.printStackTrace();
			} catch (Exception e) {
				
				e.printStackTrace();
			}
		}
	}
	
	private void startMainActivity() {

		Intent mainIntent = new Intent(WelcomeActivity.this, MainActivity.class);
		WelcomeActivity.this.startActivity(mainIntent);
		WelcomeActivity.this.finish();
	}

	private void setFullScreen() {
		requestWindowFeature(1);
		getWindow().setFlags(1024, 1024);
	}
}
